/**
 * @file: WeighingRecordServiceImpl
 * @author: michael
 * @date: 2017/8/10 10:28
 * @copyright: 南京凯盛
 */
package cn.com.kisen.kdc.service.weighing;

import cn.com.kisen.kdc.domain.StatisData;
import cn.com.kisen.kdc.domain.weighing.SimpleWeighingRecord;
import cn.com.kisen.kdc.mapper.weighing.WeighingRecordMapper;
import com.kisen.common.core.persistence.utils.Condition;
import com.kisen.common.core.utils.DateUtil;
import core.common.dto.SimpleListResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 *
 * @author michael
 * @version 1.0 Created on 2017/8/10 10:28
 */
@Service
public class WeighingRecordServiceImpl implements WeighingRecordService{


    @Autowired
    private WeighingRecordMapper mapper;

    @Override
    public Double sum(int inout, String materialName, long startTime,
            long endTime) {
      List<StatisData> statDataList =  mapper.sum(inout, materialName, startTime, endTime);
      if (statDataList.size() == 1 && statDataList.get(0) != null) {
          return ((BigDecimal)statDataList.get(0).getValue()).doubleValue();
      }
      return null;
    }

    @Override
    public List<StatisData> sumByday(int inout, String materialName, long startTime, long endTime){
        return mapper.sumByday(inout ,materialName, startTime, endTime);
    }

    @Override
    public List<StatisData> sum(int inout, String materialName, int field, Date dateTime) {
        if (field == 2) {
            return mapper.sumBy(inout, materialName, "%Y-%m-%d", DateUtil.df6.format(dateTime));
        } else if (field == 3) {
            return mapper.sumBy(inout, materialName, "%Y-%m", DateUtil.df6.format(dateTime));
        } else if (field == 4) {
            return mapper.sumBy(inout, materialName, "%Y", DateUtil.df6.format(dateTime));
        } else {
            return null;
        }
    }

    @Override
    public List<SimpleWeighingRecord> getTruck(int inout, long startTime, long endTime) {
        List<Condition> conditions = new ArrayList<>();
        if (inout == 0) {
            conditions.add(new Condition("startTime", ">=", startTime, "param1"));
            conditions.add(new Condition("startTime", "<", endTime, "param2"));
        }else{
            conditions.add(new Condition("endTime", ">=", startTime, "param1"));
            conditions.add(new Condition("endTime", "<", endTime, "param2"));
        }
        List<SimpleWeighingRecord> result = new ArrayList<>();
        /*List<WeighingRecord> weighingRecords =  mapper.listAndSort(null, conditions);
        for (WeighingRecord weighingRecord : weighingRecords) {
            result.add(new SimpleWeighingRecord(weighingRecord));
        }*/
        return result;
    }

    @Override
    public Long getTruckCount(long startTime, long endTime) {
        return mapper.count(startTime, endTime);
    }

    @Override
    public SimpleListResult getTruckData(long startTime, long endTime) {
        SimpleListResult<SimpleWeighingRecord> simpleListResult = new SimpleListResult();
        List<SimpleWeighingRecord> simpleWeighingRecords = mapper.list(startTime, endTime);
        simpleListResult.setData(simpleWeighingRecords);
        simpleListResult.setTotal(simpleWeighingRecords.size());
        return simpleListResult;
    }
}
